home *** CD-ROM | disk | FTP | other *** search
/ Clickx 75 / Clickx 75.iso / software / expressionweb / expressionwebv3 / ExpressionWeb_en.exe / Setup / WeNoLoc.cab / xweb.FPCLASS.FPDBNET.CS < prev    next >
Encoding:
Text File  |  2009-06-09  |  35.0 KB  |  1,090 lines

  1. using System;
  2. using System.Configuration;
  3. using System.Collections;
  4. using System.Collections.Specialized;
  5. using System.Data;
  6. using System.Data.OleDb;
  7. using System.Globalization;
  8. using System.Text;
  9. using System.Web;
  10. using System.Web.Caching;
  11. using System.Web.SessionState;
  12. using System.Web.UI;
  13. using System.Web.UI.WebControls;
  14. using System.Web.UI.HtmlControls;
  15.  
  16. namespace Microsoft.Office.FrontPage.DBInterface 
  17. {
  18.     /* This class implements the basic functionality of connecting to a database, providing DataSets 
  19.      * and DataViews on the connection, and managing the cached version of the DataSet.
  20.      */
  21.     public class DBConnection
  22.     {
  23.         private OleDbConnection _dBConn = null;
  24.         private OleDbDataAdapter _dataAdpater = null;
  25.         private HttpServerUtility _server = null;
  26.         private Page fpPage = null;
  27.         private Cache _cache;
  28.         private int _maxRecordCount;
  29.         private string _cacheKey;
  30.         private string _dBName;
  31.         private string _tableName;
  32.         private string _tableKey;
  33.         private string _rootPath;
  34.         private string _searchQuery;
  35.         private ArrayList _paramList;
  36.  
  37.         public string CacheName         
  38.         {
  39.             get{
  40.                 if(_cacheKey == null)
  41.                     _cacheKey = DBName;
  42.                 return _cacheKey;
  43.             }
  44.             set{
  45.                 _cacheKey = value;
  46.             }
  47.         }
  48.                 
  49.         public string DBName
  50.         {
  51.             get{
  52.                 if(_dBName == null)
  53.                     _dBName = ConfigurationSettings.AppSettings["DBName"];
  54.                 return _dBName;
  55.             }
  56.             set{
  57.                 _dBName = value;
  58.             }
  59.         }
  60.  
  61.         public int MaxRecordCount
  62.         {
  63.             get{
  64.                 return _maxRecordCount;
  65.             }
  66.             set{
  67.                 _maxRecordCount = value;
  68.             }
  69.         }
  70.  
  71.         public string RootPath
  72.         {
  73.             get{
  74.                 if(_rootPath == null)
  75.                     _rootPath = ConfigurationSettings.AppSettings["RootPath"];
  76.                 return _rootPath;
  77.             }
  78.             set{
  79.                 _rootPath = value;
  80.             }
  81.         }
  82.  
  83.         public string SearchQuery
  84.         {
  85.             get{
  86.                 if(_searchQuery == null)
  87.                     BuildSearchQuery();
  88.                 return _searchQuery;
  89.             }
  90.             set{
  91.                 _searchQuery = value;
  92.             }
  93.         }
  94.         
  95.         public string TableName
  96.         {
  97.             get{
  98.                 if(_tableName == null )
  99.                     _tableName = ConfigurationSettings.AppSettings["TableName"];
  100.                 if(_tableName == null || _tableName.Length == 0)
  101.                     _tableName = "Table";
  102.                 return _tableName;
  103.             }
  104.             set{
  105.                 _tableName = value;
  106.             }
  107.         }
  108.  
  109.         public string TableKey
  110.         {
  111.             get{
  112.                 if(_tableKey == null)
  113.                     _tableKey = ConfigurationSettings.AppSettings["TableKey"];
  114.                 if(_tableKey == null || _tableKey.Length == 0)
  115.                     SetTableKeyFromDataSet();
  116.                 return _tableKey;
  117.             }
  118.             set{
  119.                 _tableKey = value;
  120.             }
  121.         }
  122.  
  123.         
  124.         
  125.         public DBConnection(Page parentPage)
  126.         {
  127.             CacheName = "WorkingDataSet";
  128.             fpPage = parentPage;
  129.         }
  130.  
  131.     
  132.         private HttpServerUtility GetServer()
  133.         {
  134.             if(_server == null)
  135.             {
  136.                 _server = fpPage.Server;
  137.             }
  138.  
  139.             return _server;
  140.         }
  141.                 
  142.         private Cache GetCache()
  143.         {
  144.             if(_cache == null)
  145.             {
  146.                 _cache = fpPage.Cache;
  147.             }        
  148.             return _cache;
  149.         }
  150.  
  151.         
  152.  
  153.         public virtual DataSet GetDataSet(bool useCache)
  154.         {
  155.             if(useCache && HasCache())
  156.             {
  157.                 return (DataSet)GetCache()[CacheName];
  158.             }
  159.             else
  160.             {
  161.                 DataSet theDataSet = new DataSet();
  162.                 theDataSet.Locale = CultureInfo.InvariantCulture;
  163.  
  164.                 if(_dataAdpater == null)
  165.                     GetConnection();
  166.  
  167.                 ApplyParameters();
  168.                 if(MaxRecordCount > 0)
  169.                 {
  170.                     _dataAdpater.Fill(theDataSet, 0, MaxRecordCount, TableName);
  171.                 }
  172.                 else
  173.                 {
  174.                     _dataAdpater.Fill( theDataSet );
  175.                 }
  176.  
  177.                 if(useCache)
  178.                     GetCache()[CacheName] = theDataSet;
  179.  
  180.                 return theDataSet;
  181.             }
  182.         }
  183.  
  184.         private void SetTableKeyFromDataSet()
  185.         {
  186.             if( TableKey == null )
  187.             {
  188.                 TableKey = GetDataSet(true).Tables[0].Columns[0].ColumnName;
  189.             }
  190.         }
  191.  
  192.         public void RefreshCache()
  193.         {
  194.             GetCache()[CacheName] = GetDataSet(false);
  195.         }
  196.  
  197.         public bool HasCache()
  198.         {
  199.             return (GetCache()[CacheName] != null && GetCache()[CacheName] is DataSet);
  200.         }
  201.  
  202.         public DataView GetDataView( )
  203.         {
  204.             return GetDataSet( true ).Tables[0].DefaultView;
  205.         }
  206.  
  207.         public DataView GetDataView( string rowFilter, string sortFilter )
  208.         {
  209.             return new DataView( GetDataSet( true ).Tables[ 0 ], rowFilter, sortFilter, DataViewRowState.Unchanged );
  210.         }
  211.  
  212.         public virtual bool SaveChangesToDatabase()
  213.         {
  214.             if(_dataAdpater == null)
  215.                 GetConnection();
  216.  
  217.             try{
  218.                 _dataAdpater.Update(GetDataSet(true));
  219.             }
  220.             catch( System.Exception ex){
  221.                 throw new ApplicationException(ex.Message);
  222.             }
  223.             return true;
  224.         }
  225.  
  226.         protected void BuildSearchQuery()
  227.         {
  228.             StringBuilder sb = new StringBuilder("SELECT * FROM ");
  229.  
  230.             sb.Append("[");
  231.             sb.Append(TableName);
  232.             sb.Append("]");
  233.  
  234.             SearchQuery = sb.ToString();
  235.         }
  236.  
  237.         protected virtual void GetConnection()
  238.         {
  239.             if( DBName == null )
  240.                 throw new ApplicationException( "<%IDS_DBREGION_ASPNET_ERROR_NO_CONN_NAME%>" );
  241.  
  242.             String strProvider = ConfigurationSettings.AppSettings[ DBName + "_Provider" ];
  243.             String strDataSource;
  244.             String strDatabase = null;
  245.             String strUsername = ConfigurationSettings.AppSettings[ DBName + "_RuntimeUsername" ];
  246.             String strPassword = ConfigurationSettings.AppSettings[ DBName + "_RuntimePassword" ];
  247.             String strAddition = ConfigurationSettings.AppSettings[ DBName + "_AdditionalParams" ];
  248.  
  249.             if(strProvider == null)
  250.                 throw new 
  251.                     ApplicationException( "<%IDS_DBREGION_ASPNET_ERROR_NO_CONN_INFO%>");
  252.             
  253.             if(strProvider.Equals("Microsoft.Jet.OLEDB.4.0"))
  254.             {
  255.                 strDataSource = GetServer().MapPath( RootPath + ConfigurationSettings.AppSettings[DBName + "_DataSource"] );
  256.             }
  257.             else
  258.             {
  259.                 strDataSource = ConfigurationSettings.AppSettings[DBName + "_DataSource"];
  260.                 strDatabase = ConfigurationSettings.AppSettings[DBName + "_Database"];
  261.             }
  262.  
  263.             StringBuilder strConString = new StringBuilder("Provider=");
  264.             strConString.Append( strProvider );
  265.             strConString.Append( ";Data Source=" );
  266.             strConString.Append( strDataSource );
  267.             if( strDatabase != null && strDatabase.Length != 0 )
  268.                {
  269.                 strConString.Append("; Database=" );
  270.                 strConString.Append( strDatabase);
  271.                 }
  272.             if( strUsername != null && strUsername.Length != 0 )
  273.                {
  274.                 strConString.Append(  "; User ID=" );
  275.                 strConString.Append( strUsername);
  276.                 }
  277.             if( strPassword != null && strPassword.Length != 0 )
  278.                {
  279.                 strConString.Append( "; Password=" ); 
  280.                 strConString.Append( strPassword ); 
  281.                 }
  282.             if( strAddition != null && strAddition.Length != 0 )
  283.                { 
  284.                 strConString.Append( "; " );
  285.                 strConString.Append( strAddition );
  286.                }
  287.  
  288.             _dBConn = new OleDbConnection(strConString.ToString());
  289.             _dataAdpater = new OleDbDataAdapter(SearchQuery, _dBConn);
  290.             OleDbCommandBuilder OdcbCommand = new OleDbCommandBuilder(_dataAdpater);
  291.  
  292.             try{
  293.                 _dBConn.Open();
  294.                 DataTable literalInfo = _dBConn.GetOleDbSchemaTable(OleDbSchemaGuid.DbInfoLiterals, null);
  295.                 OdcbCommand.QuotePrefix = (String)literalInfo.Select("LiteralName='Quote_Prefix'")[0]["LiteralValue"];
  296.                 OdcbCommand.QuoteSuffix = (String)literalInfo.Select("LiteralName='Quote_Suffix'")[0]["LiteralValue"];
  297.             }
  298.             catch( OleDbException ex ){
  299.                 throw new ApplicationException( "<%IDS_DBREGION_ASPNET_ERROR_CONN_FAILED%>", ex );
  300.             }
  301.             finally{
  302.                 _dBConn.Close();
  303.             }
  304.         }
  305.  
  306.        
  307.         public void SetConnectionParameters(ArrayList parameterList)
  308.         {
  309.             _paramList = parameterList;
  310.         }
  311.  
  312.         protected void ApplyParameters()
  313.         {
  314.             if(_paramList == null)
  315.                 return;
  316.             if(_dataAdpater == null)
  317.                 return;
  318.             OleDbParameterCollection CommandParams = _dataAdpater.SelectCommand.Parameters;
  319.             CommandParams.Clear();
  320.             int i;
  321.             for(i=0;i<_paramList.Count;i++)
  322.             {
  323.                 CommandParams.Add(new OleDbParameter("@P"+i, _paramList[i]));
  324.             }
  325.         }
  326.     }
  327.  
  328.     /* This class provides a basic level of interaction between a page designed to work against a database
  329.                                                              * and the database itself.  It has the ability to read data from controls found in the page and place them
  330.                                                              * in a DataRow for insertion into the database.
  331.                                                              */
  332.     public abstract class DBInterfacePage : System.Web.UI.Page
  333.     {
  334.         private DBConnection _connection;
  335.         private System.Web.UI.WebControls.Label _pageErrorLabel;
  336.         private DBRegionBase _dBRegion;
  337.  
  338.         private StringCollection _fieldDefaultsCollection;
  339.         private StringCollection _fieldFriendlyNamesCollection;
  340.         private StringCollection _fieldNamesCollection;
  341.         private StringCollection _fieldTypesCollection;
  342.         private bool DebugOn = false;
  343.         private string DefaultFieldValues;
  344.         private string FriendlyNamesOfFields;
  345.         private string NamesOfFields;
  346.         private string TypesOfFields;
  347.  
  348.         public DBConnection Connection
  349.         {
  350.             get{
  351.                 return _connection;
  352.             }
  353.         }
  354.  
  355.         public DBRegionBase DBRegion
  356.         {
  357.             get{
  358.                 return _dBRegion;
  359.             }
  360.             set{
  361.                 _dBRegion = value;
  362.             }
  363.         }    
  364.         public string FieldDefaults
  365.         {
  366.             get{                
  367.                 return DefaultFieldValues;
  368.             }
  369.             set{
  370.                 DefaultFieldValues = value;
  371.             }
  372.         }
  373.         public string FieldNames
  374.         {
  375.             get{
  376.                 return NamesOfFields;
  377.             }
  378.             set{
  379.                 NamesOfFields = value;
  380.             }
  381.         }
  382.  
  383.         public string FieldFriendlyNames
  384.         {
  385.             get{
  386.                 return FriendlyNamesOfFields;
  387.             }
  388.             set{
  389.                 FriendlyNamesOfFields = value;
  390.             }
  391.         }
  392.  
  393.         public string FieldTypes
  394.         {
  395.             get{
  396.                 return TypesOfFields;
  397.             }
  398.             set{
  399.                 TypesOfFields = value;
  400.             }
  401.         }
  402.     
  403.         public System.Web.UI.WebControls.Label PageErrorTextLabel
  404.         {
  405.             get{
  406.                 return _pageErrorLabel;
  407.             }
  408.             set{
  409.                 _pageErrorLabel = value;
  410.             }
  411.         }
  412.  
  413.         public DBInterfacePage()
  414.         {
  415.             _connection = new DBConnection(this);
  416.         }
  417.  
  418.         
  419.  
  420.         protected override void OnInit( EventArgs e )
  421.         {
  422.             base.OnInit(e);
  423.             if( DBRegion != null )
  424.                 DBRegion.SetConnection( _connection );
  425.         }
  426.  
  427.         protected void BindData()
  428.         {
  429.             if( DBRegion != null )
  430.             {
  431.                 DBRegion.DataBind();
  432.             }
  433.         }
  434.  
  435.         public void ErrorMessage(string errorText)
  436.         {
  437.             if(PageErrorTextLabel == null)
  438.             {
  439.                 return;
  440.             }
  441.  
  442.             
  443.             if(DebugOn)
  444.             {
  445.                 if(PageErrorTextLabel.Text.Length == 0)
  446.                 {
  447.                     PageErrorTextLabel.Text = "<B><%IDS_DBREGION_ASPNET_DEBUG_MESSAGES_LABEL%></B><br>";
  448.                 }
  449.                 PageErrorTextLabel.Text += errorText + "<br>";
  450.             }
  451.             else
  452.             {
  453.                 PageErrorTextLabel.Text = 
  454.                     "<%IDS_DBREGION_ERROR_DEFAULT_MESSAGE%>";
  455.             }
  456.         }
  457.  
  458.         public void ResetErrorMessages()
  459.         {
  460.             if(PageErrorTextLabel != null)
  461.             {
  462.                 PageErrorTextLabel.Text = "";
  463.             }
  464.         }
  465.  
  466.         public virtual void UseControls()
  467.         {
  468.             _fieldNamesCollection = (StringCollection)Session["FieldNames"];
  469.             _fieldFriendlyNamesCollection = (StringCollection)Session["FieldFriendlyNames"];
  470.             _fieldTypesCollection = (StringCollection)Session["FieldTypes"];
  471.             _fieldDefaultsCollection = (StringCollection)Session["FieldDefaults"];
  472.  
  473.             if(_fieldNamesCollection == null || _fieldTypesCollection == null  || _fieldFriendlyNamesCollection == null)
  474.             {
  475.                 GetFieldData();
  476.             }
  477.         }
  478.  
  479.         protected virtual void  GetFieldData()
  480.         {
  481.             string sFieldNameList;
  482.             string sFieldFriendList;
  483.             string sFieldTypeList;
  484.             string sFieldDefaultsList;
  485.             if( FieldNames != null )
  486.             {
  487.                 sFieldNameList = FieldNames;
  488.             }
  489.             else
  490.             {
  491.                 sFieldNameList = ConfigurationSettings.AppSettings["FieldNames"];
  492.             }
  493.             if( FieldFriendlyNames != null)
  494.             {
  495.                 sFieldFriendList = FieldFriendlyNames;
  496.             }
  497.             else
  498.             {
  499.                 sFieldFriendList = ConfigurationSettings.AppSettings["FieldFriendlyNames"];
  500.             }
  501.             if( FieldTypes != null )
  502.             {
  503.                 sFieldTypeList = FieldTypes;
  504.             }
  505.             else
  506.             {
  507.                 sFieldTypeList = ConfigurationSettings.AppSettings["FieldTypes"];
  508.             }
  509.             if( FieldDefaults != null )
  510.             {
  511.                 sFieldDefaultsList = FieldDefaults;
  512.             }
  513.             else
  514.             {
  515.                 sFieldDefaultsList = ConfigurationSettings.AppSettings["FieldDefaults"];
  516.             }
  517.             
  518.             
  519.             int nNameComma, nFriendComma, nTypeComma, nDefaultComma;
  520.  
  521.             _fieldNamesCollection = new StringCollection();
  522.             _fieldFriendlyNamesCollection = new StringCollection();
  523.             _fieldTypesCollection = new StringCollection();
  524.            
  525.             if(sFieldDefaultsList != null)
  526.             {
  527.                 _fieldDefaultsCollection = new StringCollection();
  528.             }
  529.  
  530.             while((nNameComma=sFieldNameList.IndexOf(",")) != -1 
  531.                 && (nFriendComma = sFieldFriendList.IndexOf(",")) != -1
  532.                 && (nTypeComma=sFieldTypeList.IndexOf(",")) != -1 )
  533.             {
  534.                 if(_fieldDefaultsCollection != null)
  535.                 {
  536.                     if((nDefaultComma=sFieldDefaultsList.IndexOf(",")) == -1)
  537.                     {
  538.                         break;
  539.                     }
  540.                         
  541.                     _fieldDefaultsCollection.Add(sFieldDefaultsList.Substring(0,nDefaultComma));
  542.                     sFieldDefaultsList = sFieldDefaultsList.Substring(nDefaultComma+1);
  543.                     if(sFieldDefaultsList == null)
  544.                     {
  545.                         sFieldDefaultsList = string.Empty;
  546.                     }
  547.                 }
  548.                 _fieldNamesCollection.Add(sFieldNameList.Substring(0,nNameComma));
  549.                 sFieldNameList = sFieldNameList.Substring(nNameComma+1);
  550.                 _fieldFriendlyNamesCollection.Add(sFieldFriendList.Substring(0,nFriendComma));
  551.                 sFieldFriendList = sFieldFriendList.Substring(nFriendComma+1);
  552.                 _fieldTypesCollection.Add(sFieldTypeList.Substring(0,nTypeComma));
  553.                 sFieldTypeList = sFieldTypeList.Substring(nTypeComma+1);
  554.             }
  555.             _fieldNamesCollection.Add(sFieldNameList);
  556.             _fieldFriendlyNamesCollection.Add(sFieldFriendList);
  557.             _fieldTypesCollection.Add(sFieldTypeList);
  558.             
  559.             if(_fieldDefaultsCollection != null)
  560.                 _fieldDefaultsCollection.Add(sFieldDefaultsList);
  561.  
  562.             Session["FieldNames"] = _fieldNamesCollection;
  563.             Session["FieldFriendNames"] = _fieldFriendlyNamesCollection;
  564.             Session["FieldTypes"] = _fieldTypesCollection;
  565.             Session["FieldDefaults"] = _fieldDefaultsCollection;
  566.  
  567.             if( sFieldNameList.IndexOf(",") != -1 &&
  568.                 sFieldFriendList.IndexOf(",") != -1 &&
  569.                 sFieldTypeList.IndexOf(",") != -1 &&
  570.                 ( _fieldDefaultsCollection!=null && sFieldDefaultsList.IndexOf(",") != -1 ) )
  571.             {
  572.                 ErrorMessage("<%IDS_DBREGION_ASPNET_ERROR_FIELDS_MISMATCH%>");
  573.             }
  574.         }
  575.  
  576.         private string GetControlValue( Control controlParent, int index )
  577.         {
  578.             Control ctl = controlParent.FindControl( _fieldTypesCollection[index] + _fieldFriendlyNamesCollection[index] );
  579.             if( ctl == null && (_fieldTypesCollection[index] + _fieldFriendlyNamesCollection[index]).Length != 0 )
  580.             {
  581.                 ErrorMessage( "<%IDS_DBREGION_ASPNET_ERROR_NOCONTROLFOUND%>" );
  582.                 return string.Empty;
  583.             }
  584.             if(_fieldTypesCollection[index].Equals("txt"))
  585.             {
  586.                 return ((TextBox)ctl).Text;
  587.             }
  588.             else if(_fieldTypesCollection[index].Equals("rdo"))
  589.             {
  590.                 ListItem item = ((RadioButtonList)ctl).SelectedItem;
  591.                 if(item != null)
  592.                 {
  593.                     return item.Value;
  594.                 }
  595.                 else 
  596.                 {
  597.                     return null;
  598.                 }
  599.             }
  600.             else if(_fieldTypesCollection[index].Equals("ddl"))
  601.             {
  602.                 ListItem item = ((DropDownList)ctl).SelectedItem;
  603.                 if(item != null)
  604.                 {
  605.                     return item.Value;
  606.                 }
  607.                 else
  608.                 {
  609.                     return null;
  610.                 }
  611.             }
  612.             else if(_fieldTypesCollection[index].Equals("chk"))
  613.             {  
  614.                     return ((CheckBox)ctl).Checked ? "True" : "False" ;
  615.             }
  616.  
  617.             return string.Empty;
  618.         }
  619.  
  620.         private void SetControlValue( Control parent, int index, string sValue )
  621.         {
  622.             Control control = parent.FindControl( _fieldTypesCollection[index] + _fieldFriendlyNamesCollection[index] );
  623.             if( control == null && (_fieldTypesCollection[index] + _fieldFriendlyNamesCollection[index]).Length == 0 )
  624.             {
  625.                 ErrorMessage( "<%IDS_DBREGION_ASPNET_ERROR_NOCONTROLFOUND%>" );
  626.                 return;
  627.             }
  628.  
  629.             if(_fieldTypesCollection[index].Equals("txt"))
  630.                 ((TextBox)control).Text = sValue;
  631.             else if(_fieldTypesCollection[index].Equals("rdo"))
  632.             {
  633.                 if(((RadioButtonList)control).SelectedIndex != -1)
  634.                 {
  635.                     ((RadioButtonList)control).Items[((RadioButtonList)control).SelectedIndex].Selected = false;
  636.                 }
  637.  
  638.                 if( sValue.Length == 0 )
  639.                 {
  640.                     ((RadioButtonList)control).SelectedIndex = -1;
  641.                     return;
  642.                 }
  643.                 ListItem item = ((RadioButtonList)control).Items.FindByValue( sValue );
  644.                 if(item != null)
  645.                 {
  646.                     item.Selected = true;
  647.                 }
  648.                 else
  649.                 {
  650.                     ErrorMessage( "<%IDS_DBREGION_ASPNET_ERROR_NOT_AN_OPTION%>");
  651.                 }
  652.             }
  653.             else if(_fieldTypesCollection[index].Equals("ddl"))
  654.             {
  655.                 if( sValue.Length == 0 )
  656.                 {
  657.                     ((DropDownList)control).SelectedIndex = -1;
  658.                     return;
  659.                 }
  660.                 
  661.                 if (((DropDownList)control).SelectedIndex != -1)
  662.                 {
  663.                     ((DropDownList)control).Items[((DropDownList)control).SelectedIndex].Selected = false;
  664.                 }
  665.  
  666.                 ListItem item = ((DropDownList)control).Items.FindByValue( sValue );
  667.                 if(item != null)
  668.                 {
  669.                     item.Selected = true;
  670.                 }
  671.                 else
  672.                 {
  673.                     ErrorMessage("<%IDS_DBREGION_ASPNET_ERROR_NOT_AN_OPTION%>");
  674.                 }
  675.             }
  676.             else if(_fieldTypesCollection[index].Equals("chk"))
  677.             {
  678.                
  679.                 ((CheckBox)control).Checked = Convert.ToBoolean( sValue, CultureInfo.InvariantCulture );
  680.             }
  681.         }
  682.  
  683.         public void FillRowWithControls(DataRow row, Control controlParent)
  684.         {
  685.             FillRowWithControls( row, controlParent, false );
  686.         }
  687.  
  688.         public bool FillRowWithControls(DataRow row, Control parent, bool checkUniqueKey)
  689.         {
  690.             if(_fieldNamesCollection == null || _fieldTypesCollection == null)
  691.                 return false;
  692.  
  693.             if(checkUniqueKey)
  694.             {
  695.                 for( int i = 0; i < _fieldNamesCollection.Count; i++ )
  696.                     if( _fieldNamesCollection[i].Equals( _connection.TableKey ) )
  697.                     {
  698.                         string sKeySearch = "[" + _connection.TableKey + "]='" 
  699.                             + GetControlValue( parent, i ) + "'";
  700.                         if( _connection.GetDataSet(true).Tables[0].Select( sKeySearch ).GetLength( 0 ) != 0 )
  701.                         {
  702.                             return false;
  703.                         }
  704.                         else
  705.                         {
  706.                             break;
  707.                         }
  708.                     }
  709.             }
  710.  
  711.             for(int i=0; i<_fieldNamesCollection.Count; i++)
  712.             {
  713.                 try{
  714.                     if( _fieldNamesCollection[i].Length == 0 )
  715.                         continue;
  716.  
  717.                     string curString = GetControlValue( parent, i );
  718.                     if( ( curString != null ) && ( curString.Length != 0 ) )
  719.                     {
  720.                         row[_fieldNamesCollection[i]] = curString;
  721.                     }
  722.                     else
  723.                     {
  724.                         row[_fieldNamesCollection[i]] = DBNull.Value;
  725.                     }
  726.                 }
  727.                 catch( FormatException ex ){
  728.                     ErrorMessage("<%IDS_DBREGION_ASPNET_ERROR_WRITING%>");
  729.                 }
  730.             }
  731.  
  732.             return true;
  733.         }
  734.  
  735.         public void FillControlsWithRow(Control controlParent, DataRow row)
  736.         {
  737.             if(_fieldNamesCollection == null || _fieldTypesCollection == null)
  738.                 return;
  739.             
  740.             int i=0;
  741.  
  742.             try{
  743.                 for(; i<_fieldNamesCollection.Count; i++)
  744.                 {
  745.                     if( _fieldNamesCollection[i].Length == 0 )
  746.                         continue;
  747.  
  748.                     SetControlValue( controlParent, i, row[_fieldNamesCollection[i]].ToString() );
  749.                 }
  750.             }
  751.             catch( FormatException ex ){
  752.                 ErrorMessage( "<%IDS_DBREGION_ASPNET_ERROR_READING%>" );
  753.             }
  754.         }
  755.  
  756.         public void FillControlsWithDefaults(Control controlParent)
  757.         {
  758.             if(_fieldNamesCollection == null || _fieldTypesCollection == null)
  759.             {
  760.                 return;
  761.             }
  762.  
  763.             int i = 0;
  764.             if(_fieldDefaultsCollection == null)
  765.             {
  766.                 ErrorMessage("<%IDS_DBREGION_ASPNET_ERROR_NO_DEFAULTS%>");
  767.                 _fieldDefaultsCollection = new StringCollection();
  768.                 for(i = 0; i<_fieldNamesCollection.Count; i++)
  769.                     _fieldDefaultsCollection.Add("");
  770.             }
  771.  
  772.             try{
  773.                 for(i = 0; i<_fieldNamesCollection.Count; i++)
  774.                 {
  775.                     if( _fieldNamesCollection[i].Length == 0 )
  776.                     {
  777.                         continue;
  778.                     }
  779.  
  780.                     SetControlValue( controlParent, i, _fieldDefaultsCollection[i] );
  781.                 }
  782.             }
  783.             catch( FormatException ex ){
  784.                 ErrorMessage( "<%IDS_DBREGION_ASPNET_ERROR_READING%>");
  785.             }
  786.         }
  787.     }
  788.  
  789.     /* This class provides additional functionality over DBInterfacePage in order to enable the user to do full
  790.                                                              * edits and deletes of data as well as 
  791.                                                              */
  792.     public abstract class DBEditor : DBInterfacePage
  793.     {
  794.         
  795.         private Button _addNewRecordButton;
  796.         private Button _cancelChangeButton;
  797.         private Button _resetRecordButton;
  798.         private Button _submitChangeButton;
  799.         private DataGrid _resultsGrid;
  800.         
  801.         private HtmlInputButton _deleteRecordButton;
  802.         private HtmlGenericControl _editorPanel;
  803.         private HtmlGenericControl _keyEditPanel;
  804.     
  805.         
  806.        
  807.         public Button AddNewRecordButton
  808.         {
  809.             get{
  810.                 return _addNewRecordButton;
  811.             }
  812.             set{
  813.                 _addNewRecordButton = value;
  814.             }
  815.         }
  816.         
  817.         public Button CancelChangeButton
  818.         {
  819.             get{
  820.                 return _cancelChangeButton;
  821.             }
  822.             set{
  823.                 _cancelChangeButton = value;
  824.             }
  825.         }
  826.         
  827.         public Button ResetRecordButton
  828.         {
  829.             get{
  830.                 return _resetRecordButton;
  831.             }
  832.             set{
  833.                 _resetRecordButton = value;
  834.             }
  835.         }
  836.         public Button SubmitChangeButton
  837.         {
  838.             get{
  839.                 return _submitChangeButton;
  840.             }
  841.             set{
  842.                 _submitChangeButton = value;
  843.             }
  844.         }
  845.        
  846.         
  847.         public DataGrid ResultsGrid
  848.         {
  849.             get{
  850.                 return _resultsGrid;
  851.             }
  852.             set{
  853.                 _resultsGrid = value;
  854.             }
  855.         }
  856.         
  857.         public HtmlInputButton DeleteRecordButton
  858.         {
  859.             get{
  860.                 return _deleteRecordButton;
  861.             }
  862.             set{
  863.                 _deleteRecordButton = value;
  864.             }
  865.         }
  866.         public HtmlGenericControl EditorPanel
  867.         {
  868.             get{
  869.                 return _editorPanel;
  870.             }
  871.             set{
  872.                 _editorPanel = value;
  873.             }
  874.         }
  875.         public HtmlGenericControl KeyEditPanel
  876.         {
  877.             get{
  878.                 return _keyEditPanel;
  879.             }
  880.             set{
  881.                 _keyEditPanel = value;
  882.             }
  883.         }
  884.  
  885.  
  886.         private void OnLoadPage(object sender, System.EventArgs e)
  887.         {
  888.             InitEditor();
  889.  
  890.             if(!IsPostBack)
  891.             {
  892.                 BindData();
  893.                 SetEditorState(false, false);
  894.             }
  895.             else if(!Connection.HasCache())
  896.                 Response.Redirect("session_expired.aspx");
  897.  
  898.             ResetErrorMessages();
  899.         }
  900.  
  901.         public abstract void InitEditor();
  902.  
  903.         //Sets the enabled/disabled state of the detail and editing view of the
  904.         //editor. 
  905.         //Params:
  906.         //    isEnabled determines whether or not the view should be visible and enabled
  907.         //    isAdding determine whether or not the view is adding a new record, or updating
  908.         //             an existing record.
  909.         protected void SetEditorState(bool isEnabled, bool isAdding)
  910.         {
  911.             EditorPanel.Visible = isEnabled;
  912.             AddNewRecordButton.Enabled = !isEnabled;
  913.  
  914.             if( KeyEditPanel != null )
  915.                 KeyEditPanel.Visible = isAdding;
  916.             DeleteRecordButton.Visible = !isAdding;
  917.  
  918.             if(isAdding)
  919.                 InitialValues(null);
  920.         }
  921.  
  922.         //Sets the values of all controls to their starting value.
  923.         //Params:
  924.         //    row - determines what row to base the starting values on.
  925.         //               If it's null, use FieldDefaults.
  926.         //               If it's non-null, use  
  927.         protected void InitialValues(DataRow row)
  928.         {
  929.             if(row == null)
  930.                 FillControlsWithDefaults(EditorPanel);
  931.             else
  932.                 FillControlsWithRow(EditorPanel, row);
  933.         }
  934.  
  935.         protected DataRow CurrentRow()
  936.         {
  937.  
  938.             DataView theDataView = new DataView(Connection.GetDataSet(true).Tables[0]);
  939.             theDataView.RowFilter = (String)Session["WorkingDataRow"];
  940.             return theDataView[0].Row;
  941.         }
  942.  
  943.         protected void ClickButtonAddNewRecord(Object sender, EventArgs e)
  944.         {
  945.             InitialValues(null);
  946.             
  947.             Session["AddingDataRow"] = "true";
  948.  
  949.             SetEditorState(true, true);
  950.         }
  951.  
  952.         protected void ClickButtonResetRecord(Object sender, EventArgs e)
  953.         {
  954.             try{
  955.                 if(Session["AddingDataRow"] != null)
  956.                 {
  957.                     InitialValues(null);
  958.                 }
  959.                 else
  960.                 {
  961.                     InitialValues(CurrentRow());
  962.                 }
  963.             }
  964.             catch(RowNotInTableException notInTable){
  965.                 ErrorMessage(notInTable.Message);
  966.                 SetEditorState(false, false);
  967.             }
  968.         }
  969.  
  970.  
  971.         protected void ClickButtonCancelChange(Object sender, EventArgs e)
  972.         {
  973.             Session["WorkingDataRow"] = null;
  974.             SetEditorState(false, false);
  975.         }
  976.  
  977.         protected void ClickButtonDeleteRecord(Object sender, EventArgs e)
  978.         {
  979.             try{
  980.                 DataRow row = CurrentRow();
  981.  
  982.                 if(row != null)
  983.                     row.Delete();
  984.  
  985.                 Session["WorkingDataRow"] = null;
  986.                 Connection.SaveChangesToDatabase();
  987.                 BindData();
  988.  
  989.                 SetEditorState(false, false);
  990.             }
  991.             catch(ApplicationException dbi){
  992.                 ErrorMessage (dbi.Message);
  993.             }
  994.         }
  995.  
  996.         protected void ClickButtonSubmitChange(Object sender, EventArgs e)
  997.         {
  998.             try{
  999.                 DataRow row;
  1000.                 bool isAdding = Session["AddingDataRow"] != null;
  1001.                 if(isAdding)
  1002.                 {
  1003.                     row = Connection.GetDataSet(true).Tables[0].NewRow();
  1004.                 }
  1005.                 else
  1006.                 {
  1007.                     row = CurrentRow();
  1008.                 }
  1009.                 if(row == null)
  1010.                 {
  1011.                     ErrorMessage(
  1012.                         "<%IDS_DBREGION_ASPNET_ERROR_SESSION_DAMAGED%>");
  1013.                     ClickButtonCancelChange(sender,e);
  1014.                 }
  1015.                 else
  1016.                 {
  1017.                     row.BeginEdit();
  1018.  
  1019.                     if( FillRowWithControls(row, EditorPanel, isAdding ) )
  1020.                     {
  1021.                         row.EndEdit();
  1022.                         if(isAdding)
  1023.                         {
  1024.                             Connection.GetDataSet(true).Tables[0].Rows.Add(row);
  1025.                         }                    
  1026.                         Connection.SaveChangesToDatabase();
  1027.                         Connection.RefreshCache();
  1028.                         BindData();
  1029.  
  1030.                         Session["WorkingDataRow"] = null;
  1031.                         Session["AddingDataRow"] = null;
  1032.                         SetEditorState(false, false);
  1033.                     }
  1034.                     else
  1035.                     {
  1036.                         row.CancelEdit();
  1037.                         ErrorMessage("<%IDS_DBREGION_ASPNET_ERROR_UNIQUENESS%>");
  1038.                     }
  1039.                 }
  1040.             }
  1041.             catch(ApplicationException dbi){
  1042.                 ErrorMessage (dbi.Message);
  1043.             }
  1044.         }
  1045.  
  1046.         protected void CommandImageEdit(Object sender, CommandEventArgs e)
  1047.         {
  1048.             DataView theDataView = new DataView(Connection.GetDataSet(true).Tables[0]);
  1049.             StringBuilder sb = new StringBuilder("[");
  1050.             sb.Append( Connection.TableKey );
  1051.             sb.Append( "]='");
  1052.             sb.Append( e.CommandName );
  1053.             sb.Append( "'" );
  1054.             theDataView.RowFilter = sb.ToString();
  1055.  
  1056.  
  1057.             if(theDataView.Count == 0)
  1058.             {
  1059.                 ErrorMessage("<%IDS_DBREGION_ASPNET_ERROR_ROW_NOT_IN_CACHE%>");
  1060.                 Connection.RefreshCache();
  1061.                 BindData();
  1062.             }
  1063.             else
  1064.             {    
  1065.                 InitialValues(theDataView[0].Row);
  1066.                 Session["WorkingDataRow"] = sb.ToString();
  1067.                 Session["AddingDataRow"] = null;
  1068.                 SetEditorState(true, false);
  1069.             }
  1070.         }
  1071.  
  1072.         override protected void OnInit(EventArgs e)
  1073.         {
  1074.             InitializeComponent();
  1075.             base.OnInit(e);
  1076.         }
  1077.  
  1078.         private void InitializeComponent()
  1079.         {    
  1080.             this.Load += new System.EventHandler(this.OnLoadPage);
  1081.         }
  1082.     }
  1083.  
  1084.     public abstract class DBRegionBase : System.Web.UI.UserControl 
  1085.     {
  1086.         public abstract override void DataBind();
  1087.         public abstract void SetConnection( DBConnection theConnection );
  1088.     }
  1089. }
  1090.